package cn.xiaoxiang.org.service.impl;

import cn.xiaoxiang.basic.query.BaseQuery;
import cn.xiaoxiang.basic.service.impl.BaseServiceImpl;
import cn.xiaoxiang.basic.util.MD5Utils;
import cn.xiaoxiang.basic.util.PageList;
import cn.xiaoxiang.basic.util.StrUtils;
import cn.xiaoxiang.org.domain.Employee;
import cn.xiaoxiang.org.dto.EmployeeRoleDto;
import cn.xiaoxiang.org.mapper.EmployeeMapper;
import cn.xiaoxiang.org.service.EmployeeService;
import cn.xiaoxiang.system.mapper.RoleMapper;
import cn.xiaoxiang.user.domain.Logininfo;
import cn.xiaoxiang.user.mapper.LogininfoMapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private LogininfoMapper logininfoMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Override
    public List<Employee> findemp(Long id) {
        return employeeMapper.findemp(id);
    }

    @Override
    public void add(Employee employee) {
        // 添加员工
        String salt = StrUtils.getComplexRandomString(32);
        String password = MD5Utils.encrypByMd5(salt+"1");
        employee.setSalt(salt);
        employee.setPassword(password);
        super.add(employee);
        //添加logininfo
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee,logininfo);
        logininfo.setType(0);
        logininfo.setDisable(1);
        logininfoMapper.add(logininfo);
        // 更改员工的logininfoId
        employee.setLogininfoId(logininfo.getId());
        super.update(employee);
        // 添加员工-角色中间表
        roleMapper.addMiddle(employee.getId(),employee.getRoles());
    }

    @Override
    public void update(Employee employee) {
        employeeMapper.update(employee);
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee,logininfo);
        logininfo.setType(0);
        logininfoMapper.update(logininfo);
        //先删中间表
        roleMapper.deleteEmpOrRole(employee.getId());
        //再重新添加进去
        roleMapper.addMiddle(employee.getId(),employee.getRoles());
    }
    @Override
    public PageList<Employee> queryByPage(BaseQuery query) {
        // 去除 keyword的前后空格
        if (!StringUtils.isEmpty(query.getKeyword())) {
            query.setKeyword(query.getKeyword().trim());
        }
        // 查询数量
        Integer count = employeeMapper.queryByCount(query);
        // 查询数据
        List<Employee> ts = employeeMapper.queryByPage(query);
        // 查询角色id集合
        List<Long> eIds = ts.stream().map(Employee::getId).collect(Collectors.toList());
        List<EmployeeRoleDto>  employeeRoles = employeeMapper.findRoleIdsByEIds(eIds);
        // 转成map key：empId value：rolesid集合 List<Long> roseIds
        Map<Long, List<Long>> employeeRolesMap = employeeRoles.stream()
                .collect(Collectors.groupingBy(EmployeeRoleDto::getEmployeeId,
                        Collectors.mapping(EmployeeRoleDto::getRoleId, Collectors.toList())));
        ts.forEach(t->{
            List<Long> rolesId = employeeRolesMap.get(t.getId());
            if (!CollectionUtils.isEmpty(rolesId)){
                t.setRoles(rolesId);
            }
        });

        return new PageList<Employee>(count,ts);
    }


    @Override
    public void delete(Long id) {
        Employee byId = employeeMapper.findById(id);
        if(null!=byId.getLogininfoId()){
            logininfoMapper.delete(id);
        }
        employeeMapper.delete(id);
    }


}
